package com.lishe.lishe.wxapi;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
    private static final String APP_ID="wx2d7d684bdd4f703a";
    public static final String action_success="share_success";
    public static final String action_faild="share_faild";
    public static final String action_cancle="share_cancle";


    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, false);
        api.registerApp(APP_ID);


        //注意：
        //第三方开发者如果使用透明界面来实现WXEntryActivity，需要判断handleIntent的返回值，如果返回值为false，则说明入参不合法未被SDK处理，应finish当前透明界面，避免外部通过传递非法参数的Intent导致停留在透明界面，引起用户的疑惑
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {

        }
    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        String result;

        Log.e("WXEntryActivity", "resp.getType = " + resp.getType()+",resp.errCode = "+resp.errCode+",resp.errStr = "+resp.errStr);
        Intent intent =new Intent();
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result ="分享成功";
                intent.setAction(action_success);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result ="用户取消分享";
                intent.setAction(action_cancle);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result ="ERR_AUTH_DENIED";
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                result ="ERR_UNSUPPORT";
                break;
            default:
                result ="default";
                intent.setAction(action_faild);
                break;
        }
        sendBroadcast(intent);
        finish();
        Log.e("WXEntryActivity",result);
    }



}